<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\UserModel;
use App\Models\yb\UserWaterTicketModel;
use App\Models\yb\UserWaterTicketRecordModel;
use App\Models\yb\WaterTicketModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class WaterTicketController extends Controller
{

    /**
     * Get a JWT via given credentials.
     *  水票列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function WaterTicketListing(Request $request){

        $page = $request->post('page', 1);

        $take = $request->post('take', 10);

        $data = WaterTicketModel::where(['status'=>1])->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();
        foreach($data as $key=>$val){
            $data[$key]['goods_type_name'] = GoodsTypeModel::selectValue('id',$val['goods_type_id']);
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  水票详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function WaterTicketDetail(Request $request)
    {
        $openid = $request->post('openid');
        if(empty($openid)){
            return response()->json(['code'=>'401','msg' => '请登录']);
        }

        $water_ticket_id = $request->post('water_ticket_id');

        $water_ticket_data = WaterTicketModel::where(['id'=>$water_ticket_id,'status'=>1])->first();
        if(empty($water_ticket_data)){
            return response()->json(['code'=>'201','msg' => '该水票已下架']);
        }

        $water_ticket_data['goods_type_name'] = GoodsTypeModel::selectValue('id',$water_ticket_data['goods_type_id']);

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$water_ticket_data]);

    }

    /**
     * Get a JWT via given credentials.
     *  我的水票
     * @return \Illuminate\Http\JsonResponse
     */
    public function MyWaterTicketListing(Request $request){

        $openid = $request->post('openid');
        $user_data = UserModel::where(['openid'=>$openid])->first();
        if(empty($user_data)){
            return response()->json(['code'=>'401','msg' => '请登录']);
        }

        $page = $request->post('page', 1);

        $take = $request->post('take', 10);

        $data = UserWaterTicketModel::where(['openid'=>$openid])->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  我的水票
     * @return \Illuminate\Http\JsonResponse
     */
    public function MyWaterTicketRecord(Request $request){

        $openid = $request->post('openid');

        $user_data = UserModel::where(['openid'=>$openid])->first();

        if(empty($user_data)){
            return response()->json(['code'=>'401','msg' => '请登录']);
        }

        $page = $request->post('page', 1);

        $take = $request->post('take', 10);

        $type = $request->post('type', 1);

        $data = UserWaterTicketRecordModel::where(['openid'=>$openid,'type'=>$type])->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  水票转让
     * @return \Illuminate\Http\JsonResponse
     */
    public function WaterTicketTransfer(Request $request){
        $openid = $request->post('openid');

        $user_data = UserModel::where(['openid'=>$openid])->first();

        if(empty($user_data)){
            return response()->json(['code'=>'401','msg' => '请登录']);
        }

        $phone = $request->post('phone');

        $number = $request->post('number');
        if(empty($number)){
            return response()->json(['code'=>'203','msg' => '请输入转让数量']);
        }

        $water_ticket_id = $request->post('water_ticket_id');

        $transfer_data = UserModel::where(['phone'=>$phone])->first();
        if(empty($transfer_data)){
            return response()->json(['code'=>'203','msg' => '接收人不存在']);
        }

        $water_ticket_data = WaterTicketModel::where(['id'=>$water_ticket_id])->first();

        $user_water_ticket_data = UserWaterTicketModel::where(['openid'=>$openid,'water_ticket_id'=>$water_ticket_id])->first();
        if(empty($water_ticket_data)){
            return response()->json(['code'=>'203','msg' => '水票不存在']);
        }
        if(empty($user_water_ticket_data)){
            return response()->json(['code'=>'203','msg' => '水票不足']);
        }

        if($user_water_ticket_data['number']<$number){
            return response()->json(['code'=>'203','msg' => '水票不足']);
        }

        DB::beginTransaction();
        /**扣除转增人水票明细**/
        UserWaterTicketModel::where(['user_id'=>$user_data['id']])->decrement('number',$number);

        //添加水票记录
        UserWaterTicketRecordModel::insert([
            'user_id'=>$user_data['id'],
            'openid'=>$user_data['openid'],
            'transferor_id'=>$transfer_data['id'],
            'transferor_openid'=>$transfer_data['openid'],
            'transferor_phone'=>$transfer_data['phone'],
            'water_ticket_id'=>$water_ticket_id,
            'water_ticket_name'=>$water_ticket_data['water_ticket_name'],
            'number'=>$number,
            'type'=>3,
            'status'=>0,
            'created_at'=>date('Y-m-d H:i:s',time()),
        ]);

        /**增加接收人水票明细**/
        //修改水票数
        $transfer_water_ticket_data = UserWaterTicketModel::where(['user_id'=>$transfer_data['id'],'water_ticket_id'=>$water_ticket_id])->first();

        if(empty($transfer_water_ticket_data)){
            UserWaterTicketModel::insert([
                'user_id'=>$transfer_data['id'],
                'openid'=>$transfer_data['openid'],
                'water_ticket_id'=>$water_ticket_id,
                'goods_id'=>$water_ticket_data['goods_id'],
                'water_ticket_name'=>$water_ticket_data['water_ticket_name'],
                'number'=>$number,
                'created_at'=>date('Y-m-d H:i:s',time())
            ]);
        }else{
            UserWaterTicketModel::where(['user_id'=>$transfer_data['id']])->increment('number',$number);
        }

        //添加水票记录
        UserWaterTicketRecordModel::insert([
            'user_id'=>$transfer_data['id'],
            'openid'=>$transfer_data['openid'],
            'transferor_id'=>$user_data['id'],
            'transferor_openid'=>$user_data['openid'],
            'transferor_phone'=>$user_data['phone'],
            'water_ticket_id'=>$water_ticket_id,
            'water_ticket_name'=>$water_ticket_data['water_ticket_name'],
            'number'=>$number,
            'type'=>3,
            'status'=>1,
            'created_at'=>date('Y-m-d H:i:s',time()),
        ]);
        Db::commit();

        return response()->json(['code'=>'200','msg' => '请求成功']);

    }

}
